package com.my.study.structures.linkedlist.josephus;

import com.sun.org.apache.bcel.internal.generic.INEG;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/6/10 23:44
 **/
public class Josephus {

    public static void main(String[] args) {
        CircleSingleLinkedList circleList = new CircleSingleLinkedList();
        circleList.addBoys(5);
        circleList.list();
        System.out.println("------------出圈顺序--------------");
        circleList.countBoy(2, 2, 5);

        List<Integer> queue = getResultByQueue(2, 2, 5);
        System.out.println(queue);

    }

    private static List<Integer> getResultByQueue(int start, int count, int nums) {
        ArrayList<Integer> list = new ArrayList<>();

        Queue<Integer> persons = new LinkedList<>();

        for (int i = 1; i <= nums; i++) {
            persons.add(i);
        }

        for (int i = 0; i < start - 1; i++) {
            persons.add(persons.poll());
        }

        int countP = 0;

        while (persons.size() > 1) {
            countP++;
            Integer poll = persons.poll();
            if (countP % count == 0) {
                list.add(poll);
            }
            else {
                persons.add(poll);
            }
        }

        System.out.println(persons);

        return list;
    }

}

